//------------------------------------------------------------------------------ 
// <copyright file="JQueryBaseControl.cs" company="Fm Computer">
//     Copyright (c) Fm Computer di Menatti Fulvia.  All rights reserved.
// </copyright>
//----------------------------------------------------------------------------- 

namespace FmjQuery
{
    using System;
    using System.ComponentModel;
    using System.Web.UI;
    using System.Web.UI.WebControls;

    /// <summary>
    /// Base jQuery Control
    /// </summary>
    [NonVisualControl]
    public abstract class JQueryBaseControl : WebControl, IjQueryControl
    {
        #region Property
        /// <summary>
        /// Gets or sets client id
        /// </summary>
        public string BehaviorID
        {
            get;
            set;
        }

        /// <summary>
        /// Gets or sets JQueryManager
        /// </summary>
        [Description("JQueryManager of the page")]
        public JQueryManager Manager
        {
            get; 
            set;
        }

        /// <summary>
        /// Gets or sets a value indicating whether Render control
        /// </summary>
        [Description("Indicates if execute render")]
        public bool IsRenderControl
        {
            get;
            set;
        }
        #endregion

        #region Public Method (IjQueryCntrol)
        /// <summary>
        /// Gets all resource need include for use this control
        /// </summary>
        /// <returns>list of resource</returns>
        public abstract JWebSources GetResources();

        /// <summary>
        /// Gets list of css image resources
        /// </summary>
        /// <returns>list of images</returns>
        public abstract CssResources GetCssResources();

        /// <summary>
        /// Gets list of client nameSpace
        /// </summary>
        /// <returns>list of namespace</returns>
        public abstract ClientNameSpaces GetClientNameSpace();

        /// <summary>
        /// Create jQuery Control
        /// </summary>
        public abstract void CreateJControl();
        #endregion

        #region Override
        /// <summary>
        /// Register extend control
        /// </summary>
        /// <param name="e">argument of event</param>
        protected override void OnInit(EventArgs e)
        {
            if (DesignMode)
            {
                return;
            }

            this.RegisterWithjQueryManager();

            // Insert resource for create theme
            if (this.Manager.Theme != Themes.None)
            {
                Utility.RegisterThemeComponent(this.Page, this.GetType(), this.Manager.Theme);
            }
        }

        /// <summary>
        /// Create Jquery Control
        /// </summary>
        /// <param name="e">arguments event</param>
        protected override void OnPreRender(EventArgs e)
        {
            base.OnPreRender(e);
            this.CreateJControl();

            IjLanguages control = this as IjLanguages;

            if (control != null)
            {
                // Insert resource for create languagesS
                if (this.Manager.ManageCulture)
                {
                    Utility.RegisterLanguages(this.Page, this.GetType(), control.DefaultLanguage, control.IncludeDefaultLanguage);
                }
            }
        }

        /// <summary>
        /// Check if render control
        /// </summary>
        /// <param name="writer">Html Text Writer</param>
        protected override void Render(HtmlTextWriter writer)
        {
            if (this.IsRenderControl)
            {
                base.Render(writer);
            }

            if (DesignMode)
            {
                Literal lt = new Literal
                {
                    Text = string.Format("jQueryControl - ID : {0} - Type : {1}<br/>", this.ID, this.GetType().Name)
                };
                lt.RenderControl(writer);
            }
        }
        #endregion

        #region Private Method
        /// <summary>
        /// Register control to JQueryManager
        /// </summary>
        protected virtual void RegisterWithjQueryManager()
        {
            JQueryManager manager = JQueryManager.GetCurrent(Page);

            if (manager == null)
            {
                throw new InvalidOperationException("if you want use jQueryControl, you must insert into page JQueryManager");
            }

            this.Manager = manager;
            this.Manager.RegisterResources(this);
        }
        #endregion
    }
}